package com.rest.demo.functional.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.IntStream;

/**
 * @author Administrator
 * @decription
 * @Date 2018-01-30 17:02
 */
public class SynchronizedDemo {

    private int count = 0;

    private ReentrantLock lock = new ReentrantLock();

    private void count() {
        lock.lock();
        try {
            count++;
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        SynchronizedDemo synchronizedDemo = new SynchronizedDemo();
        ExecutorService executor = Executors.newFixedThreadPool(2);
        IntStream.range(0,100000).forEach(n -> executor.submit(synchronizedDemo::count));
        ConcurrentUtils.stop(executor);
        System.out.println(synchronizedDemo.count);
    }
}
